package com.stylefeng.guns.modular.system.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.stylefeng.guns.common.constant.Const;
import com.stylefeng.guns.common.constant.factory.PageFactory;
import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.page.PageInfoBT;
import com.stylefeng.guns.core.shiro.ShiroKit;
import com.stylefeng.guns.modular.system.service.IInitialOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 开单控制器
 *
 * @author wengys
 * @Date 2018-01-28 15:43:25
 */
@Controller
@RequestMapping("/initialOrder")
public class InitialOrderController extends BaseController {

    private String PREFIX = "/system/initialOrder/";

    @Autowired
    private IInitialOrderService initialOrderService;
    /**
     * 跳转到开单首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "initialOrder.html";
    }

    /**
     * 跳转到添加开单
     */
    @RequestMapping("/initialOrder_add")
    public String initialOrderAdd() {
        return PREFIX + "initialOrder_add.html";
    }
    /**
     * 获取开单列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(@RequestParam(required = false) String batch_no, @RequestParam(required = false) Integer deptid,Integer limit, Integer offset    ) {
       // Page<Map<String, Object>> page=new Page(offset,limit);
        Page<Map<String, Object>> page = new PageFactory<Map<String, Object>>().defaultPage();
        PageInfoBT pageInfoBT=new PageInfoBT(page);
        Map m=new HashMap();
        m.put("deptid",deptid==null? ShiroKit.getUser().getDeptId():deptid);
        m.put("batch_no",batch_no);
        m.put("dot_id",ShiroKit.getUser().getDotId());
        List<Map<String, Object>> orders = initialOrderService.selectInitialOrder(page,m);
        pageInfoBT.setRows(orders);
        pageInfoBT.setTotal(page.getTotal());
        return pageInfoBT;
    }
    /**
     * 删除开单
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer initialOrderId) {
        initialOrderService.deleteById(initialOrderId);
        return SUCCESS_TIP;
    }
    /**
     * 导出
     */
    @RequestMapping(value = "/export")
    @ResponseBody
    public Object export(HttpServletRequest request,@RequestParam(required = false) String batch_no, @RequestParam(required = false) String is_normal) {
        String dirPath = request.getSession().getServletContext().getRealPath("/") + Const.FILE_PATH;
        String dateDir = new SimpleDateFormat("yyyyMMdd").format(new java.util.Date());

        File dir = new File(dirPath + "/" + dateDir);
        if (!dir.exists()) {
            dir.mkdirs();
        }
        String datePath = "/"+new SimpleDateFormat("yyyyMMddHHmmss").format(new java.util.Date())+".xlsx";
         initialOrderService.export(dirPath + "/" + dateDir+datePath,"/"+Const.FILE_PATH+"/" + dateDir+datePath,batch_no,is_normal);
         return renderFile(new SimpleDateFormat("yyyyMMddHHmmss").format(new java.util.Date())+".xlsx",dirPath + "/" + dateDir+datePath);
    }

    /**
     * 上传图片(上传到项目的webapp/static/img)
     */
    @RequestMapping(method = RequestMethod.POST, path = "/uploadExecl")
    @ResponseBody
    public Map<String ,Object> uploadExecl(@RequestPart("file") MultipartFile multipartFile,@RequestParam(required = false) String car_no,@RequestParam(required = false) String driver_name,@RequestParam(required = false) String arrive_date,
            @RequestParam(required = false) String phone,@RequestParam(required = false) String driverId,@RequestParam(required = false) String transferId ) {
      return  initialOrderService.uploadExecl(multipartFile,car_no,driver_name,arrive_date,phone,driverId,transferId);
    }
}
